package com.c.ctgproject.ui.collect

import android.content.Intent
import android.text.Editable
import android.text.TextWatcher
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryInConfrmDetailBinding
import com.c.ctgproject.entity.guang.InWerehouseDetailEntity
import com.c.ctgproject.event.PositionEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import com.lwkandroid.imagepicker.ImagePicker
import com.lwkandroid.imagepicker.data.ImageBean
import com.lwkandroid.widget.ninegridview.NineGridBean
import org.greenrobot.eventbus.EventBus
import java.io.File
import java.math.BigDecimal

/**
 *作者:LiBW
 *创建日期: 2021/10/28
 *描述: 确认登记入库 页面 (动力电池)
 */
class BatteryInConfirmDetailActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryInConfrmDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
//    private val mData by lazy { intent.getSerializableExtra(MyConstants.DATA) as BatteryInpuOtherEntity.DataBean.ListBean }

    //图片处理
    private val imageShowList = ArrayList<String>()
    private val imageResultCode = 100

    private val mOrderId by lazy { intent.getIntExtra(MyConstants.ORDER_ID, -1) }
    private val mListPosition by lazy { intent.getIntExtra(MyConstants.POSITION,-1) }

    override fun event() {
        mBinding.mPhotoNGV.init(this, imageShowList, 3, imageResultCode)

        ApiMethods.apiSubscribe(ApiMethods.retrofit.batteryDetail(mOrderId.handlerNull()), MyObserverHandlerLoading(this, loading) {
            MyLog.d("入库单详情回调:$it")
            val mData = getGson().fromJson(it, InWerehouseDetailEntity::class.java).data
            //旧图片
            mData?.picUrls?.let { images ->
                val resultList: MutableList<NineGridBean> = ArrayList()
                images.forEach { bean ->
                    val nineGirdData = NineGridBean(bean)
                    imageShowList.add(bean)
                    resultList.add(nineGirdData)
                }
                mBinding.mPhotoNGV.addDataList(resultList)
            }

            mBinding.batteryCode.textSet(mData?.batteryCode.handlerNull())
            mBinding.batteryCount.textSet(mData?.count.handlerNull())
            mBinding.capacityAh.textSet(mData?.capacityAh.handlerNull())
            mBinding.voltage.textSet(mData?.voltage.handlerNull())
            mBinding.capacityKwh.textSet(mData?.capacityKwh.handlerNull())
            mBinding.mRemarkET.textSet(mData?.remark.handlerNull())
            mBinding.stockPosi.textSet(mData?.stockPosi.handlerNull())
        })
    }


    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
            if (imageShowList.isEmpty()){
                showToast("请上传照片")
            }

//            if (mBinding.batteryCount.text.handlerNull().isEmpty()) {
//                showToast("请输入电池数量")
//                mBinding.mCountLL.myShake()
//                return@setOnClickListener
//            }




            //所有本地文件的图片
            val filePathList = ArrayList<String>()
            imageShowList.forEach {
                if (!it.startsWith("http")) {
                    filePathList.add(it)
                }
            }

            if (filePathList.isNotEmpty()) {
                val fileList = ArrayList<File>()
                var index = 0
                filePathList.forEach {
                    index++
                    val file = File(it)
                    //图片压缩
//                            val newFile: File = CompressHelper.getDefault(this).compressToFile(file)
//                    val newFile = ImageUtil.imageWaterMark(this, file, index)
                    val newFile: File = WaterRemark.handlerWaterRemark2File(this, file, index.toString())
                    fileList.add(newFile)
                }
                //将文件传给服务器后获取地址
                upDataFile(fileList) {
                    val dataList = ArrayList<String>()
                    it?.forEach { b ->
                        dataList.add(b.fileUrl.handlerNull())
                    }
                    //旧的也给他
                    imageShowList.forEach { bean ->
                        if (bean.startsWith("http")) {
                            dataList.add(bean)
                        }
                    }

                    submitToHttp(dataList)
                }
            } else {
                //没有新文件 但是有旧的url
                if (imageShowList.isNotEmpty()) {
                    submitToHttp(imageShowList)
                } else {
                    submitToHttp(arrayListOf())
                }
            }
        }

        mBinding.capacityKwh.handlerDecimal3()

        mBinding.voltage.handlerDecimal3{
            MyLog.d(it)
            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
//        mBinding.voltage.handlerText {
//            val result = handlerKwh(it, mBinding.capacityAh.text.handlerNull())
//            result?.let {
//                mBinding.capacityKwh.textSet(result)
//            }
//        }
        mBinding.capacityAh.handlerDecimal3 {
            val result = handlerKwh(it, mBinding.voltage.text.handlerNull())
            result?.let {
                mBinding.capacityKwh.textSet(result)
            }
        }
    }

    private fun submitToHttp(imageList: ArrayList<String>) {
        if (imageList.isEmpty()){
            showToast("请上传电池照片")
            return
        }

        val map = HashMap<String, Any?>()

        val pictureList=ArrayList<Map<String,String>>()
        imageList.forEach {
            val pictureMap=HashMap<String,String>()
            pictureMap["picUrl"]=it
            pictureList.add(pictureMap)
        }
        map["orderId"] = mOrderId
        map["count"] = mBinding.batteryCount.text.handlerNull()
        map["batteryCode"] = mBinding.batteryCode.text.handlerNull()
//            map["count"] = mBinding.count.text.handlerNull()
        map["capacityAh"] = mBinding.capacityAh.text.handlerNull()
        map["voltage"] = mBinding.voltage.text.handlerNull()
        map["capacityKwh"] = mBinding.capacityKwh.text.handlerNull()
        map["remark"] = mBinding.mRemarkET.text.handlerNull()
        map["weight"]=mBinding.weight.text.handlerNull()
        map["enterPictures"] = pictureList
        map["stockPosi"]=mBinding.stockPosi.text.handlerNull()
        ApiMethods.apiSubscribe(ApiMethods.retrofit.powerBatterInConfirm(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
            MyLog.d("回调:$it")
            showToast("保存成功")
            EventBus.getDefault().post(PositionEvent(mListPosition))
            finish()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let {
            when (requestCode) {
                //图片回调
                imageResultCode -> {
                    val list: List<ImageBean>? = data.getParcelableArrayListExtra(ImagePicker.INTENT_RESULT_DATA)
                    val resultList: MutableList<NineGridBean> = ArrayList()
                    list?.forEach { imageBean ->
                        val nineGirdData = NineGridBean(imageBean.imagePath)
                        imageShowList.add(imageBean.imagePath)
                        resultList.add(nineGirdData)
                    }
                    mBinding.mPhotoNGV.addDataList(resultList)
                }
                else -> {

                }
            }
        }
    }
}